package com.epfresh.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.epfresh.R;
import com.epfresh.bean.CommentLoad;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by wzh on 2016/5/6.
 */
public class CommentNewAdapter extends BaseAdapter {

    public Context context;
    private List<CommentLoad.StoreListBean> list;
    private View.OnClickListener onItemClickListener;

    public CommentNewAdapter(Context context, ArrayList<CommentLoad.StoreListBean> list) {
        this.context = context;
        this.list = list;
    }

    public View.OnClickListener getOnItemClickListener() {
        return onItemClickListener;
    }

    public void setOnItemClickListener(View.OnClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    @Override
    public int getCount() {
        return list != null ? list.size() : 0;
    }

    @Override
    public Object getItem(int position) {
        return position;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder vh = null;
        if (convertView == null) {
            vh = new ViewHolder();
            convertView = LayoutInflater.from(context).inflate(R.layout.item_comment_new, null, false);
            vh.tv_comment_store = (TextView) convertView.findViewById(R.id.tv_comment_store);
            vh.tv_comment_go = (TextView) convertView.findViewById(R.id.tv_comment_go);
            vh.tv_comment_go.setOnClickListener(onItemClickListener);
            vh.iv_comment_1 = (ImageView) convertView.findViewById(R.id.iv_comment_1);
            vh.iv_comment_1.setOnClickListener(onItemClickListener);
            vh.iv_comment_2 = (ImageView) convertView.findViewById(R.id.iv_comment_2);
            vh.iv_comment_2.setOnClickListener(onItemClickListener);
            vh.iv_comment_3 = (ImageView) convertView.findViewById(R.id.iv_comment_3);
            vh.iv_comment_3.setOnClickListener(onItemClickListener);
            convertView.setTag(vh);
        } else {
            vh = (ViewHolder) convertView.getTag();
        }
        CommentLoad.StoreListBean bean = list.get(position);
        vh.tv_comment_store.setText(bean.getStoreName());
        vh.tv_comment_go.setTag(R.id.item_key_position,position);
        vh.tv_comment_go.setTag(R.id.item_key_status,bean.getScore());

        vh.iv_comment_1.setTag(R.id.item_key_position,position);
        vh.iv_comment_2.setTag(R.id.item_key_position,position);
        vh.iv_comment_3.setTag(R.id.item_key_position,position);
        vh.iv_comment_1.setTag(R.id.item_key_status,1);
        vh.iv_comment_2.setTag(R.id.item_key_status,2);
        vh.iv_comment_3.setTag(R.id.item_key_status,3);

        vh.iv_comment_1.setImageResource(R.mipmap.comment_1);
        vh.iv_comment_2.setImageResource(R.mipmap.comment_2);
        vh.iv_comment_3.setImageResource(R.mipmap.comment_3);
        switch (bean.getScore()){
            case 1:
                vh.iv_comment_1.setImageResource(R.mipmap.comment_1_);
                vh.tv_comment_go.setText("去吐槽");
                break;
            case 2:
                vh.iv_comment_2.setImageResource(R.mipmap.comment_2_);
                vh.tv_comment_go.setText("给点意见吧>");
                break;
            case 3:
                vh.iv_comment_3.setImageResource(R.mipmap.comment_3_);
                vh.tv_comment_go.setText("去表扬");
                break;
        }
        return convertView;
    }


    @Override
    public void notifyDataSetChanged() {
        super.notifyDataSetChanged();
    }

    public class ViewHolder {
        TextView tv_comment_store;
        ImageView iv_comment_1;
        ImageView iv_comment_2;
        ImageView iv_comment_3;
        TextView tv_comment_go;
    }
}
